package com.shamar.blog.web.backend;

import com.shamar.blog.dto.BlogContentExecution;
import com.shamar.blog.enums.BlogContentStateEnum;
import com.shamar.blog.service.BlogContentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author shamarfang
 * @date 2020-08-02 20:51
 * @desc 后台博文内容控制器
 */
@Controller
@RequestMapping("/admin")
public class ContentController {
    Logger logger = LoggerFactory.getLogger(ContentController.class);

    @Autowired
    private BlogContentService blogContentService;


    /**
     * 获取所有博文数据
     * @param request
     * @return
     */
    @GetMapping(value = "/listcontent")
    @ResponseBody
    public ModelAndView listContent(HttpServletRequest request) {
        //定义返回值
        ModelAndView  model  =  new ModelAndView("backend/content_list");
        try {
            BlogContentExecution blogContentExecution = blogContentService.getAllBlogContent();
            //数据库未出错
            if (blogContentExecution.getState() != BlogContentStateEnum.DATABASE_ERROR.getState()){
                model.addObject("blogContentList", blogContentExecution.getBlogContentList());
                return model;
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return model;
    }


}
